<template>
  <div class="search-container">

    <!-- 搜索头部 -->
    <div class="top-search-container">
      <div class="top-back" @click="onClickBack">
        <van-icon name="arrow-left" size="23px" color="gray" />
      </div>
      <div class="top-search">
        <form action="/" style="width: 100%; height: 100%">
          <van-search
            v-model="value"
            shape="round"
            show-action
            placeholder="请输入搜索关键词"
            @search="onSearch"
            @cancel="onCancel"
          />
        </form>
      </div>
    </div>

    <!-- 搜索记录 -->
    <div class="history-search-container">
      <div class="history-title"><h3>搜索记录</h3></div>
      <div class="history-remove">
        <van-icon @click="removeAll" name="delete" size="20"/>
      </div>
    </div>

    <!-- 历史记录 -->
    <div class="history-tag-item">
        <div v-for="(item, index) in history"
        class="history-item-one"
        v-if="index < 5"
        :key="index">
        {{item}}
        </div>
    </div>

    <!-- 搜索内容 -->
    <div class="history-main-container">
      <div class="coutse-list-container" v-for="item in list" :key="item.id" @click="toDetail(item.id)">
        <p class="list-title">{{ item.title }}</p>
        <p class="list-date">
          <!-- <van-icon name="clock-o" color="gray" size="15px"/> -->
          {{item.start_play_date | filterStart}}
          ~
          {{item.end_play_date | filterEnd}}
        </p>
        <!-- 第二次嵌套 -->
        <div class="list-teacher" v-for="items in item.teachers_list" :key="items.id">
          <div class="teacher-img"><img :src="items.teacher_avatar"/></div>
          <div class="teacher-name">{{item.teachers_list.teacher_name}}</div>
        </div>
        <div class="list-bottom">
            <div class="bottom-people">{{item.sales_num}}人已报名</div>
            <div class="bottom-price">{{item.price==0?"免费":item.price}}</div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { xwSearch} from "../../utils/http";
export default {
  data() {
    return {
      value: "", //保存搜索关键字
      history: [], //保存历史记录
      list: [],//保存所有的数据
      keywords: this.$route.query.value,//搜索传递过来的搜索关键字
    };
  },

  // 时间过滤器
  filters:{
    filterStart(v){
      return new Date(v).toLocaleDateString();
    },
    filterEnd(v){
      return new Date(v).toLocaleDateString();
    }
  },

  //刚进入页面从本地存储中拿到history历史记录
  mounted() {
    this.getSearch();
    this.history = JSON.parse(localStorage.getItem("history")) || [];
  },
  methods:{
    async getSearch(){
      const res = await xwSearch();
      console.log(res);
      this.list = res.list;
    },

    //返回上一页
    onClickBack() {
      window.history.go(-1);
    },

    //点击搜索
    onSearch(val) {
      this.history.unshift(this.value); //拿到搜索内容
      localStorage.history = JSON.stringify(this.history); //把搜索内容放进历史记录数组里
      //   this.$router.push({ name:"list", query: { value: this.value}});//进行页面的跳转
    },

    //点击取消按钮-弹窗
    onCancel() {
      Toast("取消");
    },

    //点击删除按钮-删除搜索历史记录
    removeAll() {
      localStorage.removeItem("history");
      this.history = [];
    },
  },
  computed: {
    searchList(){//筛选出title中含有搜索关键字的数据
      var newarr = this.list.filter((item) => {
        if (item.title.indexOf(this.keywords)!= -1) {
          return item;
        }
      });
      return newarr;
    },
  },
};
</script>

<style scoped>

/* 头部 返回 搜索输入框*/
.top-title-container {
  width: 100%;
  height: 54px;
  font-size: 20px;
  border-bottom: 1px solid lightgrey;
  display:inline-flex;
  align-items:center;
  justify-content: space-between;
  background-color:white;
}
.top-back{
  width:10%;
  height:100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}
.top-search{
  width:90%;
  height:100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}

/* 搜索记录 删除全部 */
.history-search-container {
  width: 100%;
  height: 50px;
  display: inline-flex;
  justify-content: space-between;
  align-items: center;
}
.history-search-container .history-title {
  width: 20%;
  height: 100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}
.history-search-container .history-remove {
  width: 15%;
  height: 100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin-right: 0px;
}

/* 历史记录 */
.history-tag-item{
    width:100%;
    /* height:50px; */
    border:0.3px solid whitesmoke;
    /* background-color: lightpink; */
    display: inline-flex;
    justify-content:flex-start;
    align-items: center;
    flex-wrap: wrap;
}
.history-item-one{
    margin-left:13px;
    width:47px;
    height:20px;
    line-height:20px;
    font-size:12px;
    color:black;
    background-color:whitesmoke;
    border-radius:10px;
    text-align: center;
}

/* 搜索出的内容 */
.history-main-container {
  width: 100%;
  background-color:whitesmoke;
  overflow: scroll;
}
.coutse-list-container {
  width: 92%;
  height: 170px;
  margin-left:4%;
  margin-top: 9px;
  background-color: white;
  border-radius:10px;
  position: relative;
}
.list-title{
  height:30px;
  font-size:16px;
  position: absolute;
  left:10px;
  top:9px;
}
.list-teacher{
  height:60px;
  display: inline-flex;
  justify-content: space-around;
  align-items: center;
  position: absolute;
  left:10px;
  top:67px;
}
.list-teacher .teacher-img{
  width:30px;
  height:30px;
  border-radius:50%;
}
.list-teacher .teacher-img img{
  width:100%;
  height:100%;
  border-radius:50%;
}
.list-teacher .teacher-name{
  width:150px;
  height:23px;
  color:gray;
  margin-left:3px;
  font-size:16px;
  /* background-color:lightgreen; */
}
.list-bottom{
  width:100%;
  height:40px;
  border-top:0.3px solid whitesmoke;
  display: inline-flex;
  justify-content: space-between;
  align-items: center;
  position: absolute;
  bottom:0px;
  left:0px;
}
.list-bottom .bottom-people{
  width:22%;
  height:100%;
  font-size:12px;
  display: inline-flex;
  color:gray;
  justify-content: center;
  align-items: center;
  margin-left:10px;
}
.list-bottom .bottom-price{
  width:10%;
  height:100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  color:green;
  font-size:16px;
  margin-right:10px;
}
.list-date{
  /* width:100%; */
  height:20px;
  margin-left:10px;
  position: absolute;
  top:37px;
  font-size:14px;
  color: gray;
  line-height:20px;
}

.search-container {
  width: 100vw;
  height: 100vh;
}
</style>